<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}"  media="all">
    <script th:src="@{/layui/layui.js}" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<br/>
<form class="layui-form" action="" id="searchForm">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">WeChat号</label>
            <div class="layui-input-inline">
                <input type="text" name="wechat_no" id="wechat_no" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">WeChat备注</label>
            <div class="layui-input-inline">
                <input type="text" name="wechat_mark" id="wechat_mark" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">QQ</label>
            <div class="layui-input-inline">
                <input type="text" name="qq_no" id="qq_no" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button type="button" onclick="doSearch();" class="layui-btn layui-btn-sm">Search</button>
            <button type="reset" class="layui-btn layui-btn-sm">Reset</button>
        </div>
    </div>
</form>
<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
    </div>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->

<script>
    var $,table;
    layui.use('table', function(){//加载表格模块
        table = layui.table;//获取table对象
        $ = layui.jquery;
        table.render({
            elem: '#test'
            ,url:'/courseorder/listJson'
            ,toolbar: '#toolbarDemo'
            ,title: '用户数据表'
            ,totalRow: true
            ,cols: [
                [
                {type: 'checkbox', fixed: 'left'}
                //,{field:'id', title:'ID', width:80, fixed: 'left', unresize: true, sort: true, totalRowText: '合计'}
                ,{field:'name', title:'名字', width:120}
                ,{field:'wechat_mark', title:'微信备注', width:120}
                ,{field:'wechat_no', title:'微信ID', width:120}
                ,{field:'qq_no', title:'QQ号', width:120}
                ,{field:'course_name', title:'购买课程', width:120}
                ,{field:'course_price', title:'价格', width:120}
                ,{field:'order_date', title:'订单日期', width:120}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]
            ]
            ,page: true
            ,limit:7
            ,limits:[7,8,9,10]
            ,id: 'orderTable'
        });

        //工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选')
                    break;
                case 'add' :
                    // layer.msg("弹出添加页面");
                    parent.layer.open({
                        type:2,//2跳转到页面
                        offset:'auto',
                        title:'添加课程订单',
                        content:'/courseorder/add',
                        area:['1000px','600px'],
                        btn:'关闭全部',
                        btnAlign:'c',
                        yes:function () {
                            parent.layer.closeAll();
                        }
                    });
                    break;
            };
        });

        //触发行双击事件
        table.on('rowDouble(test)', function(obj){
            //obj 同上
            var orderId = obj.data.order_id;
            // alert(orderId);

            parent.layer.open({
                type:2,//2跳转到页面
                offset:'auto',
                title:'课程订单详情',
                content:'/courseorder/detail?order_id='+ orderId,
                area:['1000px','500px'],
                btn:'关闭全部',
                btnAlign:'c',
                yes:function () {
                    parent.layer.closeAll();
                }
            });
        });

        //工具条事件
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var orderId = obj.data.order_id; //获得订单id
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）

            if(layEvent === 'del'){ //删除
                layer.confirm('真的删除行么', function(index){
                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                    layer.close(index);

                    //向服务端发送删除指令,jquery get
                    $.get("/courseorder/delete", {order_id: orderId}, function (respData) {
                        console.log(respData);
                        if (respData.success == 1) {
                            layer.msg("删除成功");
                        } else {
                            layer.msg("删除失败" + respData.message);
                        }
                    });
                });
            } else if (layEvent == 'edit') {//编辑
                //弹出编辑页面
                parent.layer.open({
                    type:2,//2跳转到页面
                    offset:'auto',
                    title:'【修改】课程订单',
                    content:'/courseorder/edit?order_id='+ orderId,
                    area:['1000px','600px'],
                    btn:'关闭全部',
                    btnAlign:'c',
                    yes:function () {
                        parent.layer.closeAll();
                    }
                });
            }
        });
    });
    
    function doSearch() {
        //通过jquery获取参数
        var wechat_no = $("#wechat_no").val();
        var wechat_mark = $("#wechat_mark").val();
        var qq_no = $("#qq_no").val();
        //table.reload方法，附加上搜索条件
        table.reload('orderTable', {
            where: {//附加额外的参数
                wechat_no:wechat_no,
                wechat_mark:wechat_mark,
                qq_no:qq_no
            },page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    }
</script>

</body>
</html>